/*
 * publicFunctions.h
 *
 *  Created on: Dec 29, 2011
 *      Author: hesham
 */

#ifndef PUBLICFUNCTIONS_H_
#define PUBLICFUNCTIONS_H_

#include "list.h"
#include "user_mutex.h"
#include "user_threads.h"

typedef enum {
	THREAD_RUNNING, THREAD_SUSPENDED, THREAD_ZOMBIE
} state;

typedef struct uthreads_db* uthreads_db;

#define UTHREAD_MAX_THREADS 256

int suspend_current_thread();

/**
 * Put thread with the specified pid into suspended state.
 * Calling this function with the tid of the current thread is the same as
 * suspend_thread() without parameters
 * Return UTHREAD_INVALID in case there is no thread with the specified tid
 * Return UTHREAD_FAIL on any other failure
 * On success return UTHREAD_SUCCESS
 */
int suspend_thread(uthread_id tid);

/**
 * Wakes up the thread with the specified tid
 * Returns UTHREAD_INVALID in case the thread doesn't exists
 * Returns UTHREAD_FAIL for any other failure
 * Returns UTHREAD_SUCCESS if the thread was waken up (or already active)
 */
int wakeupSuspended(uthread_id tid);

int mutexQClean(uthread_id thread);

/**
 * mask the signal for switching between thread
 * make sure to unmask in the end of the critical code
 */
inline void disableSignals();

/**
 * unmask the signal for switching between thread
 * make sure to unmask in the end of the critical code
 */
inline void enableSignals();

void enableSignals();

void disableSignals();

void initialize_db();

void sig_handler(int signum);

void resched(int sugnum);

int resched_flag();

void reshced_turnoff();

int get_next_ready();

void thread_start();

void change_state(uthread_id id, state st);

#endif /* PUBLICFUNCTIONS_H_ */
